package com.example.demo.service;

import com.example.demo.converter.ClothConverter;
import com.example.demo.converter.UserConverter;
import com.example.demo.converter.UserDetailConverter;
import com.example.demo.dao.User;
import com.example.demo.dao.UserDetail;
import com.example.demo.dao.UserDetailRepository;
import com.example.demo.dao.UserRepository;
import com.example.demo.dto.ClothDTO;
import com.example.demo.dto.UserDetailDTO;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;

@Service
public class UserDetailServiceImpl implements UserDetailService {

    @Autowired
    private UserDetailRepository userDetailRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private ImageService imageService;

    @Override
    public UserDetailDTO getUserDetailByUserId(long userId) {
        UserDetail userDetail = userDetailRepository.findByUserId(userId);
        try {
            UserDetailDTO userDetailDTO = UserDetailConverter.convertUserDetail(userDetail);
            String imageName = userDetailDTO.getImageName(); // 假设 ClothDTO 中有 getImageName 方法
            if (imageName != null && !imageName.isEmpty()) {
                byte[] imageBytes = imageService.getImageAsBytes(imageName);
                userDetailDTO.setImageBytes(imageBytes); // 假设 ClothDTO 有 setImageBytes 方法
            }
            return userDetailDTO;
        } catch (IOException e) {
            throw new ServiceException("Error retrieving image for Cloth", e);
        } catch (Exception e) {
            throw new ServiceException("Error converting Cloth to ClothDTO", e);
        }
    }

    @Override
    public Long addNewUserDetailByUserId(long userId, UserDetailDTO userDetailDTO) {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new RuntimeException("User not found for id: " + userId));
        userDetailDTO.setUserDTO(UserConverter.convertUser(user));
        UserDetail userDetail = userDetailRepository.save(UserDetailConverter.convertUserDetail(userDetailDTO));

        return userDetail.getId();
    }

    @Override
    public void deleteUserDetailByUserId(long userId) {
        UserDetail userDetail = userDetailRepository.findByUserId(userId);
        userDetailRepository.delete(userDetail);
    }

    @Override
    public UserDetailDTO updateUserDetailByUserId(long userId, String imageName, String bio, long followersCount, int followingCount, String gender, String birthday, String location, String school) {
        UserDetail userDetailInDB = userDetailRepository.findByUserId(userId);

        if (StringUtils.hasLength(bio) && !bio.equals(userDetailInDB.getBio())) {
            userDetailInDB.setBio(bio);
        }
        if (followersCount != userDetailInDB.getFollowersCount()) {
            userDetailInDB.setFollowersCount(followersCount);
        }
        if (followingCount != userDetailInDB.getFollowingCount()) {
            userDetailInDB.setFollowingCount(followingCount);
        }
        if (StringUtils.hasLength(imageName) && !imageName.equals(userDetailInDB.getImageName())) {
            userDetailInDB.setImageName(imageName);
        }
        if (StringUtils.hasLength(gender) && !gender.equals(userDetailInDB.getGender())) {
            userDetailInDB.setGender(gender);
        }
        if (StringUtils.hasLength(birthday) && !birthday.equals(userDetailInDB.getBirthday())) {
            userDetailInDB.setBirthday(birthday);
        }
        if (StringUtils.hasLength(location) && !location.equals(userDetailInDB.getLocation())) {
            userDetailInDB.setLocation(location);
        }
        if (StringUtils.hasLength(school) && !school.equals(userDetailInDB.getSchool())) {
            userDetailInDB.setSchool(school);
        }

        UserDetail userDetail = userDetailRepository.save(userDetailInDB);
        return UserDetailConverter.convertUserDetail(userDetail);
    }
}
